<?php

namespace App\Console\Commands;

use App\Datas\ModelList;
use App\Tools\FunTool;
use Illuminate\Console\Command;

use App\Tools\File;
use Illuminate\Support\Facades\DB;

class ImportModelVideoCommand extends BaseCommand
{
    /**
     * 命令行执行命令
     * @var string
     */
    protected $signature = 'importModelVideo';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'import model video list';

	/**
	 * 导入模型数据
	 */
    public function handle() {
        $file = $this->rootPath."/storage/tmp/model_video.csv";
		$list = File::getFileLines($file,2,25);

        $allData = DB::table(ModelList::getInstance()->tableName)->where(["img2img" =>1])->get(["id","name"])->toArray();

        //设置随机尺寸
        //foreach ($allData as $item) {
        //    list($w,$h) = FunTool::getHeightAndWidthByResolution(rand(1,3));
        //    ModelList::getInstance()->updateFiledById($item->id,[
        //        "width_v" => $w,
        //        "height_v" => $h
        //    ]);
        //}
        //return
        
        $formatAllData = [];
        foreach ($allData as $item) {
            $formatAllData[$item->name] = $item->id;
        }

        foreach ($list as $item) {
            $item = trim($item);
            if (strlen($item) < 3) {
                continue;
            }
            $itemArr = explode(",",$item);
            $name = $itemArr[0];
            $attrArr = explode("*",$itemArr[1]);
            if (array_key_exists($name,$formatAllData)) {
                $id = $formatAllData[$name];
                ModelList::getInstance()->updateFiledById($id,[
                    //"video" => "video/{$name}.mp4",
                    'width_v' => intval($attrArr[0]),
                    'height_v' => intval($attrArr[1]),
                ]);

            } else {
                $this->runLog($name);
            }
        }
    }
}